package com.gym.emp.services.impl;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gym.emp.dao.EmpMapper;
import com.gym.emp.entity.Emp;
import com.gym.emp.services.EmpServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ming
 * 2021/11/19
 * description:
 */
@Service
public class EmpServicesImpl implements EmpServices {

    @Autowired
    private EmpMapper empMapper;

    /**
     *@author:ming
     *@Date: 2021/11/19
     *@description:测试用
     */
    @Override
    public List<Emp> getAllEmp() {
        return empMapper.getAllEmp();
    }

    /**
     *@author:ming
     *@Date: 2021/11/19
     *@description:登录
     */
    @Override
    public Emp login(String ename, String pwd) {
        if (!StringUtils.isEmpty(ename)|| !StringUtils.isEmpty(pwd)) {
            //访问dao
            Emp emp = empMapper.getEmpByName(ename);
            if (emp != null && emp.getPwd().equals(pwd)) {
                return emp;
            }
        }
        return null;
    }

    /**
     *@author:ming
     *@Date: 2021/11/19
     *@description:手机号查询
     */
    @Override
    public Emp getEmpByPhone(String phone) {
        if (!StringUtils.isEmpty(phone)) {
            /* 数据库没有设置手机号唯一,故取第一个值使用 */
            return empMapper.getEmpByPhone(phone).get(0);
        }
        return null;
    }

    /**
     *@author:ming
     *@Date: 2021/11/19
     *@description:用户名查询
     */
    @Override
    public Emp getEmpByEno(Integer eno) {
        return empMapper.getEmpByEno(eno);
    }

    /**
     *@author:ming
     *@Date: 2021/11/19
     *@description:分页显示
     */
    @Override
    public PageInfo<Emp> getEmpByPage(Integer pageIndex) {
        PageHelper.startPage(pageIndex, 5);
        List<Emp> allEmp = empMapper.getAllEmp();
        PageInfo<Emp> info = new PageInfo<>(allEmp);
        return info;
    }

    /**
     *@author:ming
     *@Date: 2021/11/19
     *@description:删除
     */
    @Override
    public boolean delEmp(Integer eno) {
        if (empMapper.delEmp(eno)>0) {
            return true;
        }
        return false;
    }

    /**
     *@author:ming
     *@Date: 2021/11/19
     *@description:添加
     */
    @Override
    public boolean addEmp(Emp emp) {
        if (emp != null) {
            if (empMapper.addEmp(emp)>0) {
                return true;
            }
        }
        return false;
    }

    /**
     *@author:ming
     *@Date: 2021/11/19
     *@description:修改
     */
    @Override
    public boolean updateEmp(Emp emp) {
        if (emp != null) {
            if (empMapper.updateEmp(emp)>0) {
                return true;
            }
        }
        return false;
    }


}
